package com.atguigu.weixin.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.atguigu.weixin.kit.BasicKit;
import com.atguigu.weixin.kit.SecurtiyKit;
import com.atguigu.weixin.model.WeixinContext;
import com.atguigu.weixin.msg.MessageKit;

@Controller
@RequestMapping("/wechat")
public class WeixinController {
   public static final String TOKEN="luojianxin";
   
   
   //验证第一次的时候用到用的很少，是get请求
   @RequestMapping(value="/wget",method=RequestMethod.GET)
	public void init(HttpServletRequest req,HttpServletResponse resp) throws IOException {
	   /*signature	微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
	   timestamp	时间戳
	   nonce	随机数
	   echostr	随机字符串*/
	   
		String signature = req.getParameter("signature");
		String timestamp = req.getParameter("timestamp");
		String nonce = req.getParameter("nonce");
		String echostr = req.getParameter("echostr");
		String[] arrs = {WeixinController.TOKEN,nonce,timestamp};
		Arrays.sort(arrs);
		StringBuffer sb = new StringBuffer();
		for(String a:arrs) {
			sb.append(a);
		}
		
		String sha1 = SecurtiyKit.sha1(sb.toString());
//		System.out.println(sha1.equals(signature));
		if(sha1.equals(signature)) {
			resp.getWriter().println(echostr);
		}
		
	   
   }
   
   //2、微信接收消息都是post请求  ，例如：微信根据接收消息的内容，返回文本信息内容，文本信息的内容可以先存储到数据库中。
   //注意：事件触发的信息推送到post请求的地址上会打印出来 ：备注：所有的事件消息都会推送过来
   //注意：这个地址与接口配置信息 url地址是一样  但要是post请求
   //例如点击查看事件后就会显示:
	/*   <xml><ToUserName><![CDATA[gh_0db498be275e]]></ToUserName>
	   <FromUserName><![CDATA[oFdup00z5TSqTRmHtFhBJsP9bR1A]]></FromUserName>
	   <CreateTime>1519653542</CreateTime>
	   <MsgType><![CDATA[event]]></MsgType>
	   <Event><![CDATA[CLICK]]></Event>
	   <EventKey><![CDATA[A0001]]></EventKey>
	   </xml>
	   <xml><ToUserName><![CDATA[gh_0db498be275e]]></ToUserName>
	   <FromUserName><![CDATA[oFdup00z5TSqTRmHtFhBJsP9bR1A]]></FromUserName>
	   <CreateTime>1519653564</CreateTime>
	   <MsgType><![CDATA[event]]></MsgType>
	   <Event><![CDATA[VIEW]]></Event>
	   <EventKey><![CDATA[https://www.ljxin.top]]></EventKey>
	   <MenuId>421037389</MenuId>
	   </xml>*/
   @RequestMapping(value="/wget",method=RequestMethod.POST,produces = "text/json;charset=UTF-8")
   public void getInfo(HttpServletRequest req,HttpServletResponse resp) throws IOException{
	  //将返回消息xmll转成处理对map对象
	   Map<String,String> msgMap=MessageKit.reqMsg2Map(req);
	   System.out.println("1###@@@信息"+msgMap);
	   //将转的map对象转成符合微信返回消息xml格式的信息
	   String respCon=MessageKit.handlerMag(msgMap);
	   if(!BasicKit.isEmpty(respCon)){
		   System.out.println("2***@@@信息"+respCon);
		   resp.setContentType("application/xml; charset=utf-8");
		   resp.getWriter().write(respCon);
	   }
	   
	 /*  BufferedReader br=null;
		  br=new BufferedReader(new InputStreamReader(req.getInputStream(),"UTF-8"));
		  String str=null;
		  StringBuffer sb=new StringBuffer();
		  while((str=br.readLine())!=null){
			  System.out.println(str);
		  }*/
	   
   }
   
   
   //测试获取accesstoken
   @RequestMapping("/at")
   public void testAccessToken(HttpServletRequest req,HttpServletResponse resp) throws IOException {
	 resp.getWriter().println(WeixinContext.getAccessToken());
   }
   
   @RequestMapping(value="")
   public void handlerPost(HttpServletRequest req,HttpServletResponse resp){
	   
   }
   
}
